package wiki.old.cs236703.Wiki.Implementation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wiki.old.cs236703.Wiki.PatternType;
import wiki.old.cs236703.Wiki.ContentRanker.QueryException;


public enum QueryType{
	/*
	AND("^[\\s]*\\([\\s]*(\\\"[.]+\\\")[\\s]*\\)[\\s]*[Aa][Nn][Dd][\\s]*\\([\\s]*(\\\"[.]+\\\")[\\s]*\\)[\\s]*$") {
		@Override
		PatternType generateQuery(String query) throws QueryException {
			Matcher m = pattern.matcher(query);
			if (!m.find())
				throw new QueryException();	
			String q1 = m.group(1), q2 = m.group(2);
			PatternType pp1 = null; 
			PatternType pp2 = null;
			for (QueryType type: QueryType.values()){
				if (type.canParseQuery(q1))
					pp1 = type.generateQuery(q1);
			}
			if (pp1 == null)
				throw new QueryException();	
			for (QueryType type: QueryType.values()){
				if (type.canParseQuery(q2))
					pp2 = type.generateQuery(q2);
			}
			if (pp2 == null)
				throw new QueryException();	
			final PatternType p1 = pp1, p2 = pp2;
			return new PatternType(){					
				@Override
				public int matchScore(String str) {
					return Math.min(p1.matchScore(str), p2.matchScore(str));
				}
			};
		}
	},
	OR("^[\\s]*\\([\\s]*(\\\"[.]+\\\")[\\s]*\\)[\\s]*[oO][rR][\\s]*\\([\\s]*(\\\"[.]+\\\")[\\s]*\\)[\\s]*$") {
		@Override
		PatternType generateQuery(String query) throws QueryException {
			Matcher m = pattern.matcher(query);
			if (!m.find())
				throw new QueryException();	
			String q1 = m.group(1), q2 = m.group(2);
			PatternType pp1 = null; 
			PatternType pp2 = null;
			for (QueryType type: QueryType.values()){
				if (type.canParseQuery(q1))
					pp1 = type.generateQuery(q1);
			}
			if (pp1 == null)
				throw new QueryException();	
			for (QueryType type: QueryType.values()){
				if (type.canParseQuery(q2))
					pp2 = type.generateQuery(q2);
			}
			if (pp2 == null)
				throw new QueryException();	
			final PatternType p1 = pp1, p2 = pp2;
			return new PatternType(){					
				@Override
				public int matchScore(String str) {
					return p1.matchScore(str) + p2.matchScore(str);
				}	
			};
		}
	},
	NOT("^[\\s]*[Nn][Oo][Tt][\\s]*\\([\\s]*(\\\"[.]+\\\")[\\s]*\\)[\\s]*$") {
		@Override
		PatternType generateQuery(String query) throws QueryException  {
			Matcher m = pattern.matcher(query);
			if (!m.find())
				throw new QueryException();	
			String q = m.group(1);
			PatternType pp = null; 
			for (QueryType type: QueryType.values()){
				if (type.canParseQuery(q))
					pp = type.generateQuery(q);
			}
			if (pp == null)
				throw new QueryException();	
			final PatternType p = pp;
			return new PatternType(){					
				@Override
				public int matchScore(String str) {
					return -p.matchScore(str);
				}	
			};
		}
	},
	EXPRESSION("^[\\s]*(\\\"[.]+\\\")[\\s]*$") {
		@Override
		PatternType generateQuery(String query) throws QueryException {
			Matcher m = pattern.matcher(query);
			if (!m.find())
				throw new QueryException();	
			final String match = m.group(1);				
			return new PatternType(){					
				@Override
				public int matchScore(String str) {
					int index = 0;
					int count = 0;
					while ((index = str.indexOf(match, index) + 1) > -1)
						++count;
					return count;
				}	
			};
		}
	};		
	*/
	
//	protected Pattern[] patterns;
//	QueryType(String... regexs){
//		//for (String regexp: regexs){
//			//pattern = Pattern.compile(regexs);
//		//}
//	}
	;
	PatternType generateQuery(String query) throws QueryException{
		return null;
		
	}
	
	boolean canParseQuery(String query) {
		return false;
		//return pattern.matcher(query).find();
	}	
	
}
